<?php
/** =============================================================================
 * @name news.php
 * @date date 2017年05月30日 星期二 16时33分46秒
 * @author kaleo <kaleo1990@hotmail.com>
 * @package 
 * =============================================================================
 */
require('config.php');
//类型:top(头条，默认),shehui(社会),guonei(国内),guoji(国际),yule(娱乐),tiyu(体育)junshi(军事),keji(科技),caijing(财经),shishang(时尚)
$host = "http://toutiao-ali.juheapi.com";
$path = "/toutiao/index";
$method = "GET";
$type = $argv[1];
if (empty($type)) {
    $type="top";
}
$headers = array();
array_push($headers, "Authorization:APPCODE " . $appcode);
$querys = "type=$type";
$bodys = "";
$url = $host . $path . "?" . $querys;
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
if (1 == strpos("$".$host, "https://"))
{
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
$data = curl_exec($curl);
//关闭URL请求
curl_close($curl);
//显示获得的数据
$jsonstring=json_decode($data,true);
$news=$jsonstring['result']['data'];
$count_news=count($news);
$count_insert=0;
for ($i = 0; $i < $count_news; $i++) {
    if(empty($news[$i])){
        echo "empty";
    }
    else
    {
        $title=$news[$i]['title'];
        $date=$news[$i]['date'];
        $uniquekey=$news[$i]['uniquekey'];
        $realtype=$news[$i]['category'];
        $author_name=$news[$i]['author_name'];
        $url=$news[$i]['url'];
        $pictures_01=$news[$i]['thumbnail_pic_s'];
        $pictures_02=$news[$i]['thumbnail_pic_s02'];
        $pictures_03=$news[$i]['thumbnail_pic_s03'];
        //检查数据库中是否已经记录
        $sr=$pdo->query("select * from news where uniquekey='$uniquekey'");
        if ($row=$sr->fetch())
        {
            //已经存在了
            echo $title."已经存在了\n";
        }
        else
        {
            ////在表user_list中插入数据
            $pdo->exec("insert into news(title, date, uniquekey,type,author_name,pictures_01,pictures_02,pictures_03,url,realtype) values('$title','$date','$uniquekey','$type','$author_name','$pictures_01','$pictures_02','$pictures_03','$url','$realtype')");
            echo $title."插入了\n";
            $count_insert++;
        }
    }
}
?>
